package com.zang.blogz.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zang.blogz.annotation.OptLog;
import com.zang.blogz.constant.OptTypeConstant;
import com.zang.blogz.entity.Message;
import com.zang.blogz.modle.output.MessageVO;
import com.zang.blogz.result.Result;
import com.zang.blogz.service.MessageService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 朵橙i
 * @since 2022-06-17
 */
@RestController
@Api(tags = "留言模块")
@RequestMapping("/blogz/message")
public class MessageController {

    @Autowired
    private MessageService messageService;


    /**
     * 进行留言
     *
     * @param message 留言内容
     * @return {@link Result}<{@link Message}>
     */
    @PostMapping("/login/saveMessage")
    @OptLog(optType = OptTypeConstant.SAVE)
    @ApiOperation(value = "进行留言", notes = "进行留言")
    public Result<Message> saveMessage(@RequestParam("message") String message){

        return Result.ok(messageService.saveMessage(message));
    }

    /**
     * 查看所有留言
     *
     * @return {@link Result}<{@link List}<{@link String}>>
     */
    @GetMapping("/list")
    @ApiOperation(value = "所有留言", notes = "所有留言")
    public Result<List<String>> listMessage(){

        return Result.ok(messageService.listMessage());
    }


    /**
     * 查询管理员后台留言信息分页
     *
     * @param pageNo   当前页
     * @param pageSize 当前页大小
     * @return {@link Result}<{@link IPage}<{@link MessageVO}>>
     */
    @GetMapping("/admin/list")
    @ApiOperation(value = "查询管理员后台留言信息分页", notes = "查询管理员后台留言信息分页")
    public Result<IPage<MessageVO>> adminListMessage(@RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                     @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize){

        Page<Message> page = new Page<>(pageNo, pageSize);
        return Result.ok(messageService.adminListMessage(page));
    }


    /**
     * @param messageId 留言id
     * @return {@link Result}<{@link String}> 提示消息
     */
    @PostMapping("/admin/approved")
    @ApiOperation(value = "通过审核", notes = "通过审核")
    public Result<String> approved(@RequestParam("messageId") Integer messageId){

        return Result.ok(messageService.approved(messageId));
    }

    /**
     * 开启留言审核
     * @return {@link Result}<{@link String}> 提示消息
     */
    @PostMapping("/admin/openMessageCheck")
    @ApiOperation(value = "开启留言审核", notes = "开启留言审核")
    public Result<String> openMessageCheck(){

        return Result.ok(messageService.openMessageCheck());
    }

    /**
     * 关闭留言审核
     * @return {@link Result}<{@link String}> 提示消息
     */
    @PostMapping("/admin/downMessageCheck")
    @ApiOperation(value = "关闭留言审核", notes = "关闭留言审核")
    public Result<String> downMessageCheck(){

        return Result.ok(messageService.downMessageCheck());
    }











}
